package com.dqyt.app.service;

import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.CustomerUserBean;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.mapper.CustomerUserMapper;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class AppCustomerUserService {

    @Autowired
    private CustomerUserMapper customerUserMapper;

    /**
     * 查询所属机构下面的人员列表
     * @param request
     * @param customerId
     * @return
     */
    public ResultMsg getCuUsersByCuId(HttpServletRequest request,long userId,String token, int customerId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
             ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            List<CustomerUserBean> data= customerUserMapper.getCuUsersByCuId(customerId);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(data);
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询所属机构下面的人员列表
     * @param request
     * @param customerId
     * @return
     */
    public ResultMsg addCustomerUserApp(HttpServletRequest request,long userId,String token, String userName,String phone,
                                        int customerId,String account,String password,String password2){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }

            User user =  UserInfoUtil.getUserInfologin(userId, token);

            ret = new ResultMsg();

            //根据手机查询客户人员信息表
            CustomerUserBean customerUserBean2 =   customerUserMapper.getCustomerUserByphone(phone);
            if(null !=customerUserBean2){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("该手机号码已经注册");
                return ret;
            }

            CustomerUserBean customerUserBean3 =  customerUserMapper.getCustomerUserByAccount(account);
            if(null !=customerUserBean3){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("该账号已经存在");
                return ret;
            }
            //数据库操作
            CustomerUserBean customerUserBean = new CustomerUserBean();
            customerUserBean.setCustomerId(customerId);
            customerUserBean.setPhone(phone);
            customerUserBean.setAccount(account);
            customerUserBean.setPassword(password);
            customerUserBean.setUserName(userName);
            customerUserBean.setAvatar("/head/def_tx.png");
            customerUserBean.setStatue(1);
            customerUserBean.setAddTime(TimeUtil.currentTimeMillis());
            customerUserBean.setAddUser(user.getUserName());
            customerUserBean.setUpdateTime(TimeUtil.currentTimeMillis());
            customerUserBean.setUpdateUser(user.getUserName());
            int res = customerUserMapper.addCustomerUser(customerUserBean);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("添加成功");
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



}
